package com.zmzncs.lmtc.module.customer.controller;


import com.zmzncs.lmtc.common.pojo.tengda.TengdaForm;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.TengdaUtil;
import com.zmzncs.lmtc.module.customer.service.CardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

import static com.zmzncs.lmtc.module.customer.service.impl.CardServiceImpl.tengdaCardFormList;


/**
 * <p>
 * 客户-会员卡 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-09-02
 */
@Slf4j
@Api(tags = "客户会员卡")
@RestController
@RequestMapping("/customer/card")
public class CardController {

    @Autowired
    private TengdaUtil tengdaUtil;
    @Autowired
    private CardService cardService;

    @ApiOperation(value = "添加月租")
    @PostMapping(value = "/buy-month-card")
    public TengdaForm buyMonthCard(@RequestParam String bolianParkId, @RequestParam String carNum, @RequestParam Integer carTypeCode, @RequestParam Double amount, @RequestParam String timeStart, @RequestParam String timeEnd) throws InterruptedException {
        String uuid = UUID.randomUUID().toString();
        tengdaUtil.addCars(bolianParkId, carNum, carTypeCode, amount, timeStart, timeEnd, uuid);

        //  缓存中查询响应数据
        for (int i=0; i<10; i++) {
            Thread.sleep(500);

            //  如果uuid相同，则响应数据
            log.info("缓存会员卡数据【{}】", tengdaCardFormList);
            Optional<TengdaForm> first = tengdaCardFormList.stream().filter(tengdaForm -> tengdaForm.getUuid().equals(uuid)).findFirst();
            if(first.isPresent()){
                log.info("匹配数据【{}】", first.get());
                //  删除缓存
                cardService.deleteCache(first.get());
                return first.get();
            }
        }

        TengdaForm tengdaForm = new TengdaForm();
        tengdaForm.setCode(0);
        tengdaForm.setParkCode(bolianParkId);
        tengdaForm.setServiceId("manage.cars");
        tengdaForm.setAction("add");
        tengdaForm.setUuid(uuid);

        return tengdaForm;
    }

    @ApiOperation(value = "续月租")
    @PostMapping(value = "/month-card-renewal")
    public TengdaForm monthCardRenewal(String uuid, String bolianParkId, String carNum, Integer carTypeCode,Double amount, String timeStart,String timeEnd) throws InterruptedException {
        tengdaUtil.editCars(uuid, bolianParkId, carNum, carTypeCode, amount, DateTimeUtil.getTime(), timeEnd);

        //  缓存中查询响应数据
        for (int i=0; i<10; i++) {
            Thread.sleep(500);

            //  如果uuid相同，则响应数据
            log.info("缓存会员卡数据【{}】", tengdaCardFormList);
            Optional<TengdaForm> first = tengdaCardFormList.stream().filter(tengdaForm -> tengdaForm.getUuid().equals(uuid)).findFirst();
            if(first.isPresent()){
                log.info("匹配数据【{}】", first.get());
                //  删除缓存
                cardService.deleteCache(first.get());
                return first.get();
            }
        }

        TengdaForm tengdaForm = new TengdaForm();
        tengdaForm.setCode(0);
        tengdaForm.setParkCode(bolianParkId);
        tengdaForm.setServiceId("manage.cars");
        tengdaForm.setAction("edit");
        tengdaForm.setUuid(uuid);

        return tengdaForm;
    }

    @ApiOperation(value = "获取车牌月租")
    @GetMapping(value = "/month-card")
    public TengdaForm monthCard(@RequestParam String bolianParkId, @RequestParam String carNum) throws InterruptedException {
        String uuid = UUID.randomUUID().toString();
        tengdaUtil.monthCard(bolianParkId, carNum, uuid);

        //  缓存中查询响应数据
        for (int i=0; i<10; i++) {
            Thread.sleep(500);

            //  如果uuid相同，则响应数据
            log.info("缓存会员卡数据【{}】", tengdaCardFormList);
            Optional<TengdaForm> first = tengdaCardFormList.stream().filter(tengdaForm -> tengdaForm.getUuid().equals(uuid)).findFirst();
            if(first.isPresent()){
                log.info("匹配数据【{}】", first.get());
                //  删除缓存
                cardService.deleteCache(first.get());
                return first.get();
            }
        }

        TengdaForm tengdaForm = new TengdaForm();
        tengdaForm.setCode(0);
        tengdaForm.setParkCode(bolianParkId);
        tengdaForm.setServiceId("manage.cars");
        tengdaForm.setAction("query");
        tengdaForm.setUuid(uuid);

        return tengdaForm;
    }

}

